import { defineStore } from "pinia";
import { ref } from "vue";
import type {
  SoftwareInfo,
  CpuInfo,
  VirtualTerminalInfo,
  DeviceDetailInfo,
  DeviceBasicInfo,
  ManagementInfo
} from "@/api/hmi/version.api";
import {
  HMI_SWINFO_API,
  HMI_HWINFO_API,
  HMI_VTINFO_API,
  HMI_MCINFO_API,
  HMI_BASICINFO_API,
  HMI_MGRINFO_API
} from "@/api/hmi/version.api";

// 定义完整的设备信息响应类型
export type VersionInfoResponse = [
  SoftwareInfo[], // 软件信息数组
  CpuInfo[], // CPU信息数组
  VirtualTerminalInfo[], // 虚端子信息数组
  DeviceDetailInfo[], // 装置详细信息数组
  DeviceBasicInfo[], // 装置基本信息数组
  ManagementInfo[] // 管理单信息数组
];

export const useVersionInfoStore = defineStore("versionInfo", () => {
  // 状态
  const versionInfo = ref<VersionInfoResponse | null>(null);
  const loading = ref(false);
  const error = ref<string | null>(null);

  // 获取设备信息
  const fetchVersionInfo = async () => {
    loading.value = true;
    error.value = null;

    try {
      // 并行调用六个API接口获取设备信息
      const [
        softwareInfoResponse,
        cpuInfoResponse,
        virtualTerminalInfoResponse,
        deviceDetailInfoResponse,
        deviceBasicInfoResponse,
        managementInfoResponse
      ] = await Promise.all([
        HMI_SWINFO_API.getDeviceInfo(),
        HMI_HWINFO_API.getDeviceInfo(),
        HMI_VTINFO_API.getDeviceInfo(),
        HMI_MCINFO_API.getDeviceInfo(),
        HMI_BASICINFO_API.getDeviceInfo(),
        HMI_MGRINFO_API.getDeviceInfo()
      ]);

      // 构造响应数据
      const response: VersionInfoResponse = [
        Array.isArray(softwareInfoResponse) ? softwareInfoResponse : [softwareInfoResponse],
        Array.isArray(cpuInfoResponse) ? cpuInfoResponse : [cpuInfoResponse],
        Array.isArray(virtualTerminalInfoResponse) ? virtualTerminalInfoResponse : [virtualTerminalInfoResponse],
        Array.isArray(deviceDetailInfoResponse) ? deviceDetailInfoResponse : [deviceDetailInfoResponse],
        Array.isArray(deviceBasicInfoResponse) ? deviceBasicInfoResponse : [deviceBasicInfoResponse],
        Array.isArray(managementInfoResponse) ? managementInfoResponse : [managementInfoResponse]
      ];

      versionInfo.value = response;
    } catch (err) {
      error.value = err instanceof Error ? err.message : "获取版本信息失败";
      console.error("获取版本信息失败:", err);
    } finally {
      loading.value = false;
    }
  };

  // 获取软件信息
  const getSoftwareInfo = () => {
    return versionInfo.value?.[0] || [];
  };

  // 获取CPU信息
  const getCpuInfo = () => {
    return versionInfo.value?.[1] || [];
  };

  // 获取虚端子信息
  const getVirtualTerminalInfo = () => {
    return versionInfo.value?.[2] || [];
  };

  // 获取装置详细信息
  const getDeviceDetailInfo = () => {
    return versionInfo.value?.[3] || [];
  };

  // 获取装置基本信息
  const getDeviceBasicInfo = () => {
    return versionInfo.value?.[4] || [];
  };

  // 获取管理单信息
  const getManagementInfo = () => {
    return versionInfo.value?.[5] || [];
  };

  // 重置状态
  const resetState = () => {
    versionInfo.value = null;
    loading.value = false;
    error.value = null;
  };

  return {
    // 状态
    versionInfo,
    loading,
    error,

    // 方法
    fetchVersionInfo,
    getSoftwareInfo,
    getCpuInfo,
    getVirtualTerminalInfo,
    getDeviceDetailInfo,
    getDeviceBasicInfo,
    getManagementInfo,
    resetState,
  };
});

// 导出hook
export const useVersionInfoStoreHook = () => {
  return useVersionInfoStore();
};